package com.clean.cleantotal.bean

import android.animation.ObjectAnimator
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.net.Uri
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.databinding.BaseObservable
import coil.decode.VideoFrameDecoder
import coil.load
import coil.transform.RoundedCornersTransformation
import com.blankj.utilcode.util.ConvertUtils
import com.clean.cleantotal.R
import com.clean.cleantotal.databinding.ItemBatteryLayoutBinding
import com.clean.cleantotal.databinding.ItemBatteryScanLayoutBinding
import com.clean.cleantotal.databinding.ItemCleanLayoutBinding
import com.clean.cleantotal.databinding.ItemCleanScanLayoutBinding
import com.clean.cleantotal.databinding.ItemDeviceLayoutBinding
import com.clean.cleantotal.databinding.ItemEmptyLayoutBinding
import com.clean.cleantotal.databinding.ItemGroupLayoutBinding
import com.clean.cleantotal.databinding.ItemProcessBinding
import com.clean.cleantotal.databinding.ItemTikLayoutBinding
import com.clean.cleantotal.utils.FormatterSize
import com.drake.brv.BindingAdapter
import com.drake.brv.item.ItemBind
import com.drake.brv.item.ItemExpand
import com.drake.brv.item.ItemHover
import com.drake.brv.item.ItemPosition
import java.io.File

class DeviceBean(
    var name: String, var value: String
) : ItemBind {
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        val item = ItemDeviceLayoutBinding.bind(vh.itemView)
        item.name.text = name
        item.value.text = value
    }
}

class InfoBean(
    var icon: Int, var title: String, var value: String
) : ItemBind {
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        when (vh.itemViewType) {
            R.layout.item_battery_layout -> {
                val item = ItemBatteryLayoutBinding.bind(vh.itemView)
                item.icon.load(icon)
                item.title.text = title
                item.tvValue.text = value
            }

            R.layout.item_clean_scan_layout -> {
                val item = ItemCleanScanLayoutBinding.bind(vh.itemView)
                item.icon.load(icon)
                item.title.text = title
                when (value) {
                    "0" -> {
                        item.title.setTextColor(Color.parseColor("#9BA0A1"))
                        item.ivStatus.isVisible = true
                        item.ivLoading.isVisible = false
                        item.ivStatus.setImageResource(R.mipmap.green_clean_careful)
                    }

                    "1" -> {
                        item.title.setTextColor(Color.parseColor("#04CF7B"))
                        item.ivStatus.isVisible = false
                        item.ivLoading.isVisible = true
                        animal(item.ivLoading)
                    }

                    "2" -> {
                        item.title.setTextColor(Color.parseColor("#010101"))
                        item.ivStatus.isVisible = true
                        item.ivLoading.isVisible = false
                        item.ivStatus.setImageResource(R.mipmap.green_clean_hospital)
                    }
                }
            }
        }
    }
}

private fun animal(image: ImageView) {
    val animator = ObjectAnimator.ofFloat(image, "rotation", 0f, 360f)
    animator.duration = 1500
    animator.repeatCount = ObjectAnimator.INFINITE
    animator.repeatMode = ObjectAnimator.RESTART
    animator.start()
}

class AppInfo(
    var icon: Drawable?, var title: String, var pkName: String
) : ItemBind {
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        val item = ItemProcessBinding.bind(vh.itemView)
        item.icon.load(icon) {
            transformations(RoundedCornersTransformation(10f))
        }
        item.funTitle.text = title
    }
}

class JunkBean(
    var path: String,
    var title: String,
    var size: Long,
    var isChecked: Boolean = false,
    var drawable: Drawable? = null,
    var uri: Uri? = null,
    var childList: List<ChildBean> = emptyList(),
) : ItemBind {
    var index = -2
    var needUpdate = false
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        when (vh.itemViewType) {
            R.layout.item_empty_layout -> {
                val item = ItemEmptyLayoutBinding.bind(vh.itemView)
                item.fileName.text = title
                item.isSelect.setImageResource(if (isChecked) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)
            }

            R.layout.item_clean_layout -> {
                val item = ItemCleanLayoutBinding.bind(vh.itemView)
                item.ivSelect.setImageResource(if (isChecked) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)
                item.tvFileName.text = title
                item.tvSize.text = FormatterSize.formatFileSize(item.tvSize.context, size)
                drawable?.let {
                    item.icon.load(drawable) {
                        transformations(
                            RoundedCornersTransformation(
                                ConvertUtils.dp2px(12f).toFloat()
                            )
                        )
                    }
                } ?: run {
                    if (!path.isNullOrEmpty()) {
                        if (path.endsWith(".png") || path.endsWith(".jpg") || path.endsWith(".gif") || path.endsWith(
                                ".raw"
                            )
                        ) {
                            item.icon.load(File(path)) {
                                transformations(
                                    RoundedCornersTransformation(
                                        ConvertUtils.dp2px(12f).toFloat()
                                    )
                                )
                            }
                        } else if (path.endsWith(".mp4") || path.endsWith(".wmv") || path.endsWith(".3gp") || path.endsWith(
                                ".mov"
                            ) || path.endsWith(".avi")
                        ) {
                            item.icon.load(path) {
                                decoderFactory { result, options, _ ->
                                    VideoFrameDecoder(
                                        result.source, options
                                    )
                                }
                                transformations(
                                    RoundedCornersTransformation(
                                        ConvertUtils.dp2px(12f).toFloat()
                                    )
                                )
                            }
                        } else {
                            item.icon.load(R.mipmap.icon_other_file)
                        }
                    } else {
                        item.icon.load(R.mipmap.icon_other_file)
                    }
                }
                if (needUpdate) {
                }
                updateBg(index, item.cleanItemLayout, item.line)
            }
        }
    }
}


private fun updateBg(index: Int, layout: ConstraintLayout, line: TextView? = null) {
    when (index) {
        0 -> {
            layout.setBackgroundResource(R.drawable.w_15_top)
            line?.let {
                it.visibility = View.GONE
            }
        }

        -1 -> {
            layout.setBackgroundResource(R.drawable.w_15_bottom)
            line?.let {
                line.visibility = View.VISIBLE
            }
        }

        1 -> {
            layout.setBackgroundResource(R.drawable.w_15)
            line?.let {
                line.visibility = View.VISIBLE
            }
        }

        else -> {
            layout.setBackgroundResource(R.drawable.w_15_center)
            line?.let {
                line.visibility = View.GONE
            }
        }
    }
}


class JunkGroup(
    var title: String,
    var totalSize: Long,
    var isSelect: Boolean = false,
    var sublist: List<JunkBean> = emptyList(),
) : ItemExpand, ItemHover, ItemPosition, BaseObservable(), ItemBind {
    // 同级别分组的索引位置
    override var itemGroupPosition: Int = 0
    override var itemHover: Boolean = true
    override var itemPosition: Int = 0

    // 当前条目是否展开
    override var itemExpand: Boolean = false
        set(value) {
            field = value
            notifyChange()
        }

    override fun getItemSublist(): List<JunkBean> {
        return sublist
    }

    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        val item = ItemGroupLayoutBinding.bind(vh.itemView)
        if (itemExpand) {
            item.ivJianTou.setImageResource(R.mipmap.green_clean_hotel)
        } else {
            item.ivJianTou.setImageResource(R.mipmap.green_clean_silk)
        }

        item.ivSelect.setImageResource(if (isSelect) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)

        item.tvSize.text = FormatterSize.formatFileSize(item.tvSize.context, totalSize)
        item.title.text = title
    }
}

class TikBean(
    var icon: Int, var title: String, var path: String, var size: Long, var isSelect: Boolean
) : ItemBind {
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        val item = ItemTikLayoutBinding.bind(vh.itemView)
        item.icon.load(icon)
        item.title.text = title
        item.tvSize.text = FormatterSize.formatFileSize(item.title.context, size)
        item.ivSelect.setImageResource(if (isSelect) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)
    }
}

class StorageBean(
    val total: String,
    val used: String,
    val available: String,
    val system: String,
)

class ChildBean(
    val path: String, val size: Long
)

class FirstBean(var title: String, var result: String, var status: Int) : ItemBind {
    override fun onBind(vh: BindingAdapter.BindingViewHolder) {
        val item = ItemBatteryScanLayoutBinding.bind(vh.itemView)
        item.title.text = title
        item.result.text = result
        when (status) {
            0 -> {
                item.result.visibility = View.INVISIBLE
                item.ivLoading.visibility = View.VISIBLE
                item.ivLoading.setImageResource(R.mipmap.green_scan_simple)
                item.title.setTextColor(Color.parseColor("#9BA0A1"))
                item.ivStatus.isVisible = true
                item.ivLoading.isVisible = false
            }

            1 -> {
                item.result.visibility = View.INVISIBLE
                item.ivLoading.visibility = View.VISIBLE
                item.ivLoading.setImageResource(R.mipmap.green_scan_simple)
                item.title.setTextColor(Color.parseColor("#04CF7B"))
                item.ivStatus.isVisible = false
                item.ivLoading.isVisible = true
                animal(item.ivLoading)
            }

            2 -> {
                item.result.visibility = View.VISIBLE
                item.ivLoading.visibility = View.INVISIBLE
                item.ivLoading.setImageResource(R.mipmap.green_scan_simple)
                item.title.setTextColor(Color.parseColor("#343434"))
                item.ivStatus.isVisible = false
                item.ivLoading.isVisible = false
                animator?.cancel()
            }
        }
    }

    private var animator: ObjectAnimator? = null
    private fun animal(image: ImageView) {
        animator = ObjectAnimator.ofFloat(image, "rotation", 0f, 360f)
        animator?.duration = 1500
        animator?.repeatCount = ObjectAnimator.INFINITE
        animator?.repeatMode = ObjectAnimator.RESTART
        animator?.start()
    }

}